package com.aaa.cart.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
import java.util.Map;

/**
 * fileName:CartDao
 * description:
 * author:MT
 * createTime:2020/2/20 18:08
 * version:1.0.0
 */
public interface CartDao {

    /**
     * 查询购物车中该商品的数量
     *
     * @param map 购物车
     * @return 购物车信息
     */
    @Select("select quantity from " +
            "oms_cart_item where product_id=#{productId} and " +
            "member_id=#{memberId} and shop_id=#{shopId}")
    Integer selectCartItemQuantityById(Map map);
    /**
     * 判断商品是否已加入购物车
     *
     * @param map 购物车
     * @return 购物车信息
     */
    @Select("select * from " +
            "oms_cart_item where product_id=#{productId} and " +
            "member_id=#{memberId} and shop_id=#{shopId}")
    Integer isAddCart(Map map);

    /**
     * 查询购物车中的所有商品
     *
     * @param  map 购物车信息
     * @return 购物车集合
     */
    @Select("select id,product_id productId,member_id memberId,shop_id shopId," +
            "quantity,price,product_pic productPic,product_name productName,product_category_id productCategoryId,shopping_way shoppingWay"+
            " from oms_cart_item where member_id=#{memberId} and " +
            "shop_id=#{shopId}")
    List<Map> selectCartItemList(Map map);

    /**
     * 添加商品到购物车
     *
     * @param  map 购物车信息
     * @return 结果
     */
    @Insert("insert into oms_cart_item(product_id,member_id,shop_id,product_pic," +
            "price,product_name,product_category_id,shopping_way)values" +
            "(#{productId},#{memberId},#{shopId},#{productPic}," +
            "#{price},#{productName},#{productCategoryId},#{shoppingWay})")
    int insertCartItem(Map map);

    /**
     * 修改添加到购物车的商品数量
     *
     * @param map 购物车信息
     * @return 结果
     */
    @Update("update oms_cart_item set quantity=quantity+1 " +
            "where  product_id=#{productId} and member_id=#{memberId} and " +
            "shop_id=#{shopId}")
    int addCartItemQuantity(Map map);

//    @Select("select ")
//    boolean isExit(Map map);

    /**
     * 修改添加到购物车的商品数量
     *
     * @param map 购物车信息
     * @return 结果
     */
    @Update("update oms_cart_item set quantity=quantity-1 " +
            "where  product_id=#{productId} and member_id=#{memberId} and " +
            "shop_id=#{shopId}")
    int minusCartItemQuantity(Map map);

    /**
     * 删除购物车
     *
     * @param map
     * @return 结果
     */
    @Delete("<script>delete from oms_cart_item where member_id=#{memberId} and shop_id=#{shopId}" +
            "<if test=\"productId!= null and productId != ''\"> and product_id = #{productId} </if>"+
            "</script>")
    int deleteCartItemById(Map map);
}
